<!--商户管理-->
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves icon="el-icon-plus" :loading="downloadLoading" class="filter-item right-btn" type="primary" @click="toAdd">
        添加
      </el-button>
    </div>
    <ve-table
      style="width:100%"
      :columns="columns"
      :table-data="tableData"
      :border-x="true"
      row-key-field-name="id"
      :border-y="true"
      :row-style-option="rowStyleOption"
      :checkbox-option="checkboxOptipon"
    />
    <div class="table-pagination">
      <ve-pagination
        :total="totalCount"
        :page-index="pageIndex"
        :page-size="pageSize"
        @on-page-number-change="pageNumberChange"
        @on-page-size-change="pageSizeChange"
      />
    </div>
    <el-dialog
      class="lnk-dialog"
      :close-on-click-modal="false"
      :title="dialogTitle"
      top="10vh"
      width="30%"
      :visible.sync="newDialog"
      :append-to-body="true"
    >
      <el-form
        ref="mainForm"
        :model="formData"
        :rules="rules"
        size="mini"
        class="editForm"
      >
        <!--表单项-->
        <el-row>
          <el-col>
            <el-form-item label="关联阶段名称" prop="clientId" class="car-form-item">
              <el-select
                v-model="formData.flow"
                @change="selectFlowChange"
                filterable
                remote
                class="car-modal-col"
                placeholder="请选择阶段"
              >
                <el-option
                  v-for="item in flowStage"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="附件类型编码" prop="additionalInformation" class="car-form-item">
              <el-input v-model="formData.code" class="car-modal-col" placeholder="请输入附件类型编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="附件类型名称" prop="additionalInformation" class="car-form-item">
              <el-input v-model="formData.type" class="car-modal-col" placeholder="请输入附件类型名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="newDialog = false;$refs['mainForm'].resetFields();">取消</el-button>
        <el-button type="primary" @click="addData()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.table-pagination {
  margin-top: 20px;
  text-align: right;
}

.right-btn {
  float: right;
}

.dialog-footer {
  text-align: center;
}

.car-modal-col {
  width: 280px;
}
</style>

<script>

export default {
  data() {
    return {
      rules: {
        flow: [{ required: true, message: '必输', trigger: 'blur' }],
        code: [{ required: true, message: '必输', trigger: 'blur' }],
        type: [{ required: true, message: '必输', trigger: 'blur' }]
      }, // 表单校验规则
      tableData: [],
      totalCount: '',
      dialogTitle: '新增商户', // 弹框名称
      editorFlag: false,
      newDialog: false, // 添加弹框
      formData: {}, // 添加表单
      appId: '',
      pageIndex: 1,
      pageSize: 10,
      listQuery: {},
      flowStage:[
        {code:"order",name:"订单新增"},
        {code:"credit",name:"征信查询"},
        {code:"apply",name:"贷款申请"},
        {code:"risk",name:"风控审核"},
        {code:"sign",name:"签约管理"},
        {code:"loan",name:"银行放款"},
        {code:"pledge",name:"抵押管理"},
        {code:"close",name:"归档"},
      ],
      rowStyleOption: { // 表格样式
        stripe: true, // 斑马纹
        hoverHighlight: true, // hover高亮
        clickHighlight: true // 点击行高亮
      },
      checkboxOptipon: {
        // 行选择改变事件
        selectedRowChange: ({ row, isSelected, selectedRowKeys }) => {
          console.log(row, isSelected, selectedRowKeys)
        },
        // 全选改变事件
        selectedAllChange: ({ isSelected, selectedRowKeys }) => {
          console.log(isSelected, selectedRowKeys)
        }
      },
      columns: [
        {
          field: 'id',
          key: 'f',
          // type=checkbox
          title: '',
          width: 50,
          align: 'center'
        },
        {
          field: 'flow',
          key: 'a',
          title: '阶段名称',
          align: 'center',
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            let f = {}
            this.flowStage.map(item => {
              if(item.code == row.flow){
                f = item
              }
            })
            return(
              <span>{f.name}</span>
            )
          }
        },
        { field: 'code', key: 'b', title: '阶段代码', align: 'center' },
        { field: 'type', key: 'b', title: '附件类型', align: 'center' },
        {
          field: 'hobby', key: 'k', title: '操作',
          align: 'center',
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            return (
              <span>
                <el-button circle icon='el-icon-edit' class='filter-item' type={'primary'} on-click={() => this.editorDealer(row)}></el-button>
                <el-button circle icon='el-icon-delete' class='filter-item' type={'danger'} on-click={() => this.deleteorDealer(row)}></el-button>
              </span>
            )
          }
        }
      ]
    }
  },
  computed: {
  },
  created() {
    this.appId = sessionStorage.getItem('appId')
    this.initDatabase()
  },
  methods: {
    // page number change
    pageNumberChange(pageIndex) {
      this.pageIndex = pageIndex
      this.initDatabase()
    },

    // page size change
    pageSizeChange(pageSize) {
      this.pageIndex = 1
      this.pageSize = pageSize
      this.initDatabase()
    },

    /**
     * 初始化数据
     * @date 2021-05-10
     */
    async initDatabase() {
      this.tableData = []
      const param = { appId: await sessionStorage.getItem('appId') }
      const data = await this.$http.post('/flow/attachConfig/list/' + this.pageIndex + '/' + this.pageSize, param)
      console.log(data.data.list.records)
      if (data.code === 200) {
        this.tableData = data.data.list.records
        this.totalCount = data.data.length
      } else {
        this.$message({
          type: 'error',
          message: '查询失败，请刷新页面'
        })
      }
      console.log(this.tableData)
    },


    /**
     * 删除
     * @param {Object} row
     */
    async deleteorDealer(row) {
      const that = this

      this.$alert('是否确认删除', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const data = await that.$http.delete('/flow/attachConfig/delete/' + row.id, {})
        console.log(data)
        if (data.code === 200) {
          that.initDatabase()
        } else {
          that.$message({
            type: 'info',
            message: '操作失败，请重试'
          })
        }
      })
    },

    /**
     * 编辑
     * @date 2021-05-08
     * @param row
     */
    editorDealer(row) {
      this.formData = row
      this.newDialog = true
      this.editorFlag = true // 编辑标识
      this.dialogTitle = '编辑附件类型'
    },
    /**
     * 点击新增按钮
     * @date 2021-05-08
     */
    async toAdd() {
      this.formData = {}
      this.newDialog = true
      this.editorFlag = false // 编辑标志
      this.dialogTitle = '新增附件类型'
    },
    /**
     * 新增操作
     * @date 2021-05-08
     */
    async addData() {
      this.$refs['mainForm'].validate(async(valid) => {
        if (valid) {
          if (this.formData.id) {
            const data = await this.$http.post('/flow/attachConfig/update', this.formData)
            console.log(data)
            if (data.code === 200) {
              this.newDialog = false
              this.initDatabase()
            } else {
              this.$message({
                type: 'info',
                message: '操作失败，请重试'
              })
            }
          } else {
            this.formData.appid = this.appId
            const data = await this.$http.post('/flow/attachConfig/save', this.formData)
            console.log(data)
            if (data.code === 200) {
              this.newDialog = false
              this.initDatabase()
            } else {
              this.$message({
                type: 'info',
                message: '操作失败，请重试'
              })
            }
          }
        }
      })
    }
  }
}
</script>
